package com.demo.demoTest;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

import java.util.List;
import java.util.Map;

/**
 * @author lkz
 * @ClassName: PageUtil
 * @description: TODO
 * @date 2022/6/8 17:18
 */
public class PageUtil {



    /**
     * pagehelper   手动分页
     * @param currentPage 当前页
     * @param pageSize 每页数量
     * @param list 列表
     * @param <T> 实体类
     * @return 分页
     */
    public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize,List list) {
        int total = list.size();
        if (total > pageSize) {
            int toIndex = pageSize * currentPage;
            if (toIndex > total) {
                toIndex = total;
            }
            list = list.subList(pageSize * (currentPage - 1), toIndex);
        }
        Page<T> page = new Page<>(currentPage, pageSize);
        page.addAll(list);
        page.setPages((total + pageSize - 1) / pageSize);
        page.setTotal(total);

        PageInfo<T> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    public static void parsePageInfo (PageInfo pageFrom , PageInfo pageTo) {
        pageTo.setTotal(pageFrom.getTotal());
        pageTo.setSize(pageFrom.getSize());
        pageTo.setPageSize(pageFrom.getPageSize());
    }
}
